package com.yami.shop.admin.config;

import com.github.wxpay.sdk.WXPayConfig;
import com.yami.shop.bean.pay.PayConstant;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 微信 配置实体类
 * Created by Jason on 2018/11/20.
 */
public class WXPayConfigImpl implements WXPayConfig{

    private byte[] certData;
    private static WXPayConfigImpl wxPayConfigImpl;
    private static String cert_path;
    private static String notify_url;

    private WXPayConfigImpl(String certPath,String notifyUrl) throws Exception{
        ExceptionUtil.checkEmpty(certPath,"Sorry,Don't found your cert!"); // 校验证书路径
        ExceptionUtil.checkEmpty(notifyUrl,"Notify url can not null!");    // 校验回路径

        InputStream certStream=WXPayConfigImpl.class.getResourceAsStream("/apiclient_cert.p12");
        //File file = new File(this.getClass().getResource("/apiclient_cert.p12").getPath());
        //InputStream certStream = new FileInputStream(file);
        //this.certData = new byte[(int) file.length()];
        //certStream.read(this.certData);
        certStream.close();
    }

    /**
     * 获取实现类对象
     * @param certPath
     * @param notifyUrl
     * @return
     * @throws Exception
     */
    public static WXPayConfigImpl getInstance(String certPath,String notifyUrl) throws Exception{
        notify_url = notifyUrl;
        cert_path = certPath;
        if (wxPayConfigImpl == null) {
            synchronized (WXPayConfigImpl.class) {
                if (wxPayConfigImpl == null) {
                    wxPayConfigImpl = new WXPayConfigImpl(cert_path,notify_url);
                }
            }
        }
        return wxPayConfigImpl;
    }

    /**
     * 获取证书字节流
     * @return
     */
    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public String getAppID() {
        return PayConstant.APPID;
    }

    public String getMchID() {
        return PayConstant.MCHID;
    }

    public String getKey() {
        return PayConstant.APIKEY;
    }

    public String getNotifyUrl() {
        return notify_url;
    }

    public String getTradeType() {
        return PayConstant.TRADE_TYPE;
    }
    // 请求连接超时时长
    public int getHttpConnectTimeoutMs() {
        return PayConstant.HTTP_CONNECT_TIMEOUT;
    }
    // 请求读取超时时长
    public int getHttpReadTimeoutMs() {
        return PayConstant.HTTP_READ_TIMEOUT;
    }

    public String getPrimaryDomain() {
        return PayConstant.PRIMARY_DEMAIN;
    }

    public String getAlternateDomain() {
        return PayConstant.ALTERNATE_DEMAIN;
    }

    public int getReportWorkerNum() {
        return PayConstant.REPORT_WORKER_NUM;
    }

    public int getReportBatchSize() {
        return PayConstant.REPORT_BATCH_SIZE;
    }

    public String getSpbillCreateIp() {
        return PayConstant.BILL_CREATE_IP;
    }
}
